<?php

/**
 * 墨宝支付JDK
 * @author chenshubo
 */
namespace App\Service\VendorClient\S2bMoBaoPay;
use App\Service\Code\Normal;
use App\Service\BaseService;
use App\Service\AppService\SystemCacheConfigService;

class S2bMoBaoPayApiClient extends BaseService
{
    const account = 'mobao_pay';
    protected $config = [];
    public function __construct() {
    }
    
    /**
     * 支付接口
     * @param array $params
     * @return mixed
     */
    public function OrderPay( $params = [] )
    {
        $this->config   = SystemCacheConfigService::instance()->getCacheInfoBySystemCode(self::account);
        $orderInfo      = $params['payInfo'];
        $orderGoods     = current($params['orderGoods']);
        $order_sn   	= $orderInfo['order_sn'];
        $data = [
            'nosign' => [
                'payerIdNumber' => $orderInfo['buyer_idcard'],
                'payerName'     => $orderInfo['buyer_realname'],
                'telephone'     => $orderInfo['buyer_phone'],
                'payerIdType'   => 1,
            ],
            'sign'   => [
                'orderNo'       => $order_sn,
                'amt'           => $orderInfo['pay_money'],
                'tradeDate'     => date('Ymd'),
                'tradeSummary'  => '购物订单'.$order_sn,
                'bid'           => $orderInfo['bid'],
                'order_id'      => $orderInfo['id']
            ],
            'pay_type'  => $params['pay_type']
        ];
        $requestParam = [
            'method'    => 'orderPay',
            'data'      => $data,
        ];
        return $this->execute($requestParam);
    }
    

    //摩宝验签
    public function mobao_sign( $param=array() )
    {
        $arr = [];
        foreach ($param as $k=>$v){
            $arr[] = "{$k}={$v}";
        }
        return implode("&", $arr);
    }

    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function execute( $param = [] )  
    {
        //摩宝配置
        $config = $this->config;
        if( empty($config) ) {
            return Normal::returnCode(Normal::FAIL,"支付配置不能为空");
        }
        
        $data = $param['data'];
        if( $data['pay_type'] == 2 ) {//在线充值
            $returnUrl  = $config['scanPayReturnUrl']."?order_id={$data['sign']['order_id']}";
            $noticeUrl  = $config['scanPayNoticeUrl'];
        } else {
            $returnUrl  = $config['NotifyWebUrl']."?order_id={$data['sign']['order_id']}";
            $noticeUrl  = $config['CallbackUrl'];
        }
        $sign = [
            'apiName'           => $config['apiName'],
            'apiVersion'        => $config['apiVersion'],
            'platformID'        => $config['platformID'],
            'merchNo'           => $config['merchNo'],
            'orderNo'           => $data['sign']['orderNo'],
            'tradeDate'         => $data['sign']['tradeDate'],
            'currency'          => $config['currency'],
            'amt'               => $data['sign']['amt'],
            'merchUrl'          => $noticeUrl,
            'frontMerchUrl'     => $returnUrl,
            'merchParam'        => $config['merchParam'].'_'.$data['sign']['bid'],
            'tradeSummary'      => $data['sign']['tradeSummary'],
            'overTime'          => $config['overTime'],
            'customerIP'        => getIp(),
        ];
        $sign['signMsg']  = md5($this->mobao_sign($sign).$config['key']);
        $sign = array_merge($sign,$data['nosign']);
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'form_data' => $sign,
            'form_url'  => $config['API_URL']
        ]);
    }
}